import 'package:flutter/material.dart';

//对于商品属性进行抽象
class Product {
  final String title; //商品名称
  final String description; //商品详情
  Product(this.title, this.description);
}

class RouteDataTestRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('导航的数据传递与接收--商品列表'),
      ),
      body: ProductList(
        products: List.generate(
          20,
          (i) => Product('商品$i', '这是一个商品详情，编号$i'),
        ),
      ),
    );
  }
}

//自定义组件
class ProductList extends StatelessWidget {
  //接收参数
  final List<Product> products;

  ProductList({Key key, @required this.products}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: products.length,
      itemBuilder: (context, index) {
        return ListTile(
          title: Text(products[index].title),
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => ProductDetail(product: products[index]),
              ),
            );
          },
        );
      },
    );
  }
}

class ProductDetail extends StatelessWidget {
  //接收参数
  final Product product;

  ProductDetail({Key key, @required this.product}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(product.title),
      ),
      body: Center(
        child: Text(product.description),
      ),
    );
  }
}
